package arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author DCSGO
 * @version 1.0
 */
public class ArraysMethod {
    public static void main(String[] args) {
        //通过 Arrays.toString 将一维数组转换成字符串
        Integer[] arr = {1, 2, 3, 4};
        System.out.println(Arrays.toString(arr));//[1, 2, 3, 4]

        //通过 Arrays.deepToString 将二维数组转换成字符串
        Integer[][] arr2 = new Integer[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println(Arrays.deepToString(arr2));//[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

        //通过 Arrays.sort 将数组元素按升序排序
        Integer[] nums = {1, 0, 5, 4, 8, 2};
        Integer[] nums2 = nums.clone();

        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));//[0, 1, 2, 4, 5, 8]

        //传入接口 Comparator 实现自定义排序顺序
        //在sort内部进行元素之间的比较时就会使用我们传入的 Comparator
        Arrays.sort(nums2, new Comparator() {//匿名内部类
            @Override//重写 compare(Object o1, Object o2)
            public int compare(Object o1, Object o2) {
                Integer i1 = (Integer) o1;
                Integer i2 = (Integer) o2;
                return i2 - i1;//降序 后 - 前
            }
        });

        System.out.println(Arrays.toString(nums2));//[8, 5, 4, 2, 1, 0]
    }
}
